<?php

namespace app\common\model;

use app\common\model\order\Order;
use app\common\model\user\Level;
use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog extends Model
{

    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
        'money_text',
        'createtime_text',
    ];
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getTypeList()
    {
        return ['1' => __('收入'), '2' => __('支出')];
    }
    public function getMoneyTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['source_type']) ? $data['source_type'] : '');
        $list = $this->getMoneyType();
        return isset($list[$value]) ? $list[$value] : '';
    }
    const no_withdraw_change = [];
    const money_change = [
        500, // => '余额支付',
        501, //=> '余额充值',
        502, //=> '余额退款',
    ];
    const frozen_money_change = [];
    //返回变动字段
    public static function getChangeField($source_type)
    {

        $field = '';
        if (in_array($source_type, self::money_change)) {
            $field = 'money';
        }

        if (in_array($source_type, self::frozen_money_change)) {
            $field = 'frozen_money';
        }
        if (in_array($source_type, self::no_withdraw_change)) {
            $field = 'no_money';
        }
        return $field;
    }
    public static function getMoneyType()
    {
        return [
            500 => '余额支付',
            501 => '余额充值',
            502 => '余额退款',
        ];
    }
    /**
     * Notes:记录会员账户流水，如果变动类型是成长值，且是增加的，则调用更新会员等级方法。该方法应在添加用户账户后调用
     * @author: cjh 2020/12/15 11:49
     * @param int $user_id  用户id
     * @param float $amount 变动数量
     * @param int $change_type  变动类型：1-减少；2-增加
     * @param int $source_type 来源类型
     * @param string $source_id 来源id
     * @param string $source_sn 来源单号
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function income($user_id, $amount, $change_type, $source_type, $source_sn = '')
    {
        $user = new User;
        $user = $user->get($user_id);

        if (empty($user)) {
            return false;
        }

        $field = self::getChangeField($source_type);
        $before = $user->$field;

        if ($change_type == 1) {
            $user->setInc($field, $amount);
        } else {
            $user->setDec($field, $amount);
            $amount = -$amount;
        }
        $left_amount = $user->$field;
        $list =  self::getMoneyType();
        $account_log = new self;
        $data['type'] = $change_type;
        $data['order_sn'] = $source_sn;
        $data['user_id'] = $user_id;
        $data['money'] = $amount ?? 0;
        $data['before'] = $before ?? 0;
        $data['after'] = $left_amount ?? 0;
        $data['memo'] = $memo ?? (isset($list[$source_type]) ? $list[$source_type] : '');
        $data['source_type'] = $source_type;
        $account_log->allowField(true)->save($data);
        return true;
    }
}
